package com.lzd.main.bdmap;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BaiduMapWebApi {

    @Value("${server.baiduAk}")
    private static String baiduAk = "hnA92HK478DiWF76zpLEEGTU4umt6xlF";

    public static List<Double[]> routePointArr(String startPoint, String endPoint){

        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("origin", startPoint);
        paramMap.put("destination", endPoint);
        paramMap.put("ak", baiduAk);

        // 当无法识别页面编码的时候，可以自定义请求页面的编码
        String result = HttpUtil.get("https://api.map.baidu.com/directionlite/v1/driving", paramMap);
        //String result = HttpUtil.get("http://localhost/fangwei/luxian.json");

        //System.out.println(result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer status = jsonObject.getInt("status");
        if(status==0){
            List<Double[]> list = new ArrayList<>();
            JSONObject resultObj = jsonObject.getJSONObject("result");

            JSONArray platArr = resultObj.getJSONArray("routes");
            JSONObject stepObj = platArr.getJSONObject(0);
            JSONArray stepArr = stepObj.getJSONArray("steps");
            for (int i=0;i<stepArr.size();i++){
                Double[] tmp =  new Double[2];
                JSONObject pathObj = stepArr.getJSONObject(i);
                if(i==0){
                    JSONObject startPointObj = pathObj.getJSONObject("start_location");
                    tmp[0] = Double.parseDouble(startPointObj.getStr("lng"));
                    tmp[1] = Double.parseDouble(startPointObj.getStr("lat"));
                    //System.out.println("第1条："+tmp[0]+","+tmp[1]);
                }else{
                    JSONObject endPointObj = pathObj.getJSONObject("end_location");
                    tmp[0] = Double.parseDouble(endPointObj.getStr("lng"));
                    tmp[1] = Double.parseDouble(endPointObj.getStr("lat"));
                    //System.out.println("第n条："+tmp[0]+","+tmp[1]);
                }
                list.add(tmp);
            }

            return list;
        }
        return null;
    }

    /**
     * 输入地点，返回坐标
     * @param address
     * @return
     */
    public static Double[] getAddressPoint(String address){

        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("address", address);
        paramMap.put("output", "json");
        paramMap.put("ak", baiduAk);

        // 当无法识别页面编码的时候，可以自定义请求页面的编码
        String result = HttpUtil.get("https://api.map.baidu.com/geocoding/v3/", paramMap);
        //String result = HttpUtil.get("https://api.map.baidu.com/geocoding/v3/");

        System.out.println(result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer status = jsonObject.getInt("status");
        if(status==0){
            Double[] data = new Double[2];
            JSONObject resultObj = jsonObject.getJSONObject("result");

            JSONObject locationObj = resultObj.getJSONObject("location");
            data[0] = locationObj.getDouble("lng");
            data[1] = locationObj.getDouble("lat");
            return data;
        }
        return null;
    }
}
